package com.banciyuan.bcywebview.biz.main.mineinfo.reward;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.biz.write.photoselecotor.util.CommonUtils;
import com.banciyuan.bcywebview.utils.common.UIUtils;
import com.banciyuan.bcywebview.utils.string.StringUtil;

import java.util.List;

import de.greenrobot.daoexample.model.MyGiftItem;

/**
 * Created by hy on 2016/5/16.
 */
public class RewardAdapter extends BaseAdapter {
    private LayoutInflater mLayoutInflater;
    private List<MyGiftItem> mMyGiftItems;
    private String postTypeFormat;
    private String spaceFormat;
    private int type;//0 send type;1 receive type
    private Context mContext;
    private int maxNameWith;
    private int maxTitleWidth;

    public RewardAdapter(Context context, List<MyGiftItem> giftItems, int type) {
        mLayoutInflater = LayoutInflater.from(context);
        mContext = context;
        mMyGiftItems = giftItems;
        this.type = type;
        postTypeFormat = context.getString(R.string.feed_posttype_format);
        spaceFormat = context.getString(R.string.space_format);
        maxNameWith = CommonUtils.getWidthPixels((Activity) mContext) - UIUtils.dip2px(238, mContext);
        maxTitleWidth = CommonUtils.getWidthPixels((Activity) mContext) - UIUtils.dip2px(184, mContext);
    }

    @Override
    public int getCount() {
        return mMyGiftItems.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        MyGiftItem giftItem = mMyGiftItems.get(position);
        if (null == convertView) {
            convertView = mLayoutInflater.inflate(R.layout.reward_item, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tvCatFoodNum.setText(giftItem.getValue_coin());
        viewHolder.tvFeedItemName.setText(String.format(spaceFormat, giftItem.getPay_item_name()));
        viewHolder.tvFeedTime.setText(StringUtil.getOppositeTime(giftItem.getCtime()));
        if (type == 0) {
            viewHolder.feedPrefix.setVisibility(View.VISIBLE);
            viewHolder.tvFeederName.setText(giftItem.getTo_uname());
            viewHolder.feedSuffix.setText(getFeedSuffix(giftItem.getPost_data().getType()));
        } else {
            viewHolder.feedPrefix.setVisibility(View.GONE);
            viewHolder.tvFeederName.setText(giftItem.getFrom_uname());
            viewHolder.feedSuffix.setText(getReceiveSuffix(giftItem.getPost_data().getType()));
        }
        if (giftItem.getPost_data().isPost_del()) {
            viewHolder.tvPostType.setVisibility(View.GONE);
            viewHolder.tvPostTitle.setText(mContext.getString(R.string.post_deleted));
        } else {
            viewHolder.tvPostType.setVisibility(View.VISIBLE);
            viewHolder.tvPostType.setText(String.format(postTypeFormat, giftItem.getPost_data().getType_name()));
            viewHolder.tvPostTitle.setText(giftItem.getPost_data().getTitle());
        }
        return convertView;
    }

    private String getReceiveSuffix(String workType) {
        if ("cos".equals(workType) || "illust".equals(workType) || "novel".equals(workType))
            return mContext.getString(R.string.feeded_mywork_suffix);
        else
            return mContext.getString(R.string.feeded_mywork_daily_suffix);
    }

    private String getFeedSuffix(String workType) {
        if ("cos".equals(workType) || "illust".equals(workType) || "novel".equals(workType))
            return mContext.getString(R.string.feeded_suffix);
        else
            return mContext.getString(R.string.feeded_daily_suffix);
    }


    class ViewHolder {
        View feedPrefix;
        TextView feedSuffix;
        TextView tvFeederName;
        TextView tvCatFoodNum;
        TextView tvPostType;
        TextView tvPostTitle;
        TextView tvFeedItemName;
        TextView tvFeedTime;

        public ViewHolder(View convertView) {
            feedPrefix = convertView.findViewById(R.id.tv_feed_prefix);
            feedSuffix = (TextView) convertView.findViewById(R.id.tv_feed_suffix);
            tvFeederName = (TextView) convertView.findViewById(R.id.tv_feeder_name);
            tvCatFoodNum = (TextView) convertView.findViewById(R.id.tv_cat_food_num);
            tvPostType = (TextView) convertView.findViewById(R.id.tv_feedpost_type);
            tvPostTitle = (TextView) convertView.findViewById(R.id.tv_feedpost_title);
            tvFeedItemName = (TextView) convertView.findViewById(R.id.tv_cat_food);
            tvFeedTime = (TextView) convertView.findViewById(R.id.tv_feed_time);
            tvFeederName.setMaxWidth(maxNameWith);
            tvPostTitle.setWidth(maxTitleWidth);
        }
    }


}
